package com.blb.zy_community.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zy_community.entity.ResponseResult;
import com.blb.zy_community.entity.SysPost;
import com.blb.zy_community.entity.ZyUnit;
import com.blb.zy_community.service.ISysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 岗位信息表 前端控制器
 * </p>
 *
 * @author 二组
 * @since 2023-06-18
 */
@CrossOrigin
@RestController
public class SysPostController {
    @Autowired
    private ISysPostService postService;

    @RequestMapping("/post-page")
    public ResponseResult<Page<SysPost>> getPostPage(@RequestParam(required = false,defaultValue = "1") Long current,
                                                        @RequestParam(required = false,defaultValue = "5")Long size,
                                                        @RequestParam(required = false)String postName,
                                                        @RequestParam(required = false)String postCode,
                                                        @RequestParam(required = false)String status
    ){
        Page<SysPost> page = postService.getPostPage(current, size, postName, postCode,status);
        return ResponseResult.ok(page);
    }

    @PostMapping("/post")
    public ResponseResult<String> addPost(@RequestBody SysPost post){
        postService.save(post);
        return ResponseResult.ok("ok");
    }

    @PutMapping("/post")
    public ResponseResult<String> updatePost(@RequestBody SysPost post){
        postService.updateById(post);
        return ResponseResult.ok("ok");
    }

    @DeleteMapping("/post/{id}")
    public ResponseResult<String> updatePost(@PathVariable Long id){
        postService.removeById(id);
        return ResponseResult.ok("ok");
    }
    /**
     * 批量删除
     * @param
     * @return
     */
    @DeleteMapping("/posts/{postIds}")
    public ResponseResult<String> deletePosts(@PathVariable Long[] postIds){
        postService.deletePost(postIds);
        return ResponseResult.ok(postIds);
    }
    @RequestMapping("/postUpdate/{id}")
    public ResponseResult<String> getOneUnit(@PathVariable Long id){
        SysPost postId = postService.getOne(new QueryWrapper<SysPost>().eq("post_id", id));
        return ResponseResult.ok(postId);
    }
    @GetMapping("/postName")
    public ResponseResult<String> selectPostName(){
        List<SysPost> list = postService.list();
        return ResponseResult.ok(list);
    }

}
